import math
def get_deno(D):  # 获取分母的信息,并将其存储在deno列表中
    deno = []
    len1 = len(D)
    for i in range(len1):
        s = 1
        for j in range(len1):
            if j != i:
                s *= (D[i][0]-D[j][0])
        deno.append(s)
    return deno

def fun(x,D): 
    deno = get_deno(D)
    mole = []  # 获取分子的信息
    len1 = len(D)
    for i in range(len1):  # 获取分子信息,将其存储在mole中
        s = 1
        for j in range(len1):
            if j != i:
                s *= (x-D[j][0])
        mole.append(s)
    s = 0
    for i in range(len(mole)):  # 计算函数值并返回
        s += mole[i]/deno[i]*D[i][1]
    return s

if __name__ == "__main__":
    D = [(1, math.exp(1)), (2, math.exp(2)), (3, math.exp(3)), (4, math.exp(4))]
    y = fun(3.5, D)
    print(y)

    